package com.dingding.special.order.channel.service.impl;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;

import com.dingding.facade.special.dto.ReqChannelOrderDTO;
import com.dingding.facade.special.dto.ReqChannelOrdersDTO;
import com.dingding.facade.special.dto.ResChannelOrderDTO;
import com.dingding.facade.special.dto.ResChannelOrdersDTO;
import com.dingding.facade.special.dto.ResChannelOrdersListDTO;
import com.dingding.special.order.channel.dao.ChannelOrderDao;
import com.dingding.special.order.channel.service.ChannelOrderService;

/** 
 * @ClassName: ChannelOrderServiceImpl 
 * @Description: 旅行社后台-获取渠道订单
 * @author SUNZHICHENG 
 * @date 2016年9月12日 下午8:36:31 
 * 
*/
@Component("channelOrderService")
public class ChannelOrderServiceImpl implements ChannelOrderService{
	
	@Resource(name="channelOrderDao")
	ChannelOrderDao channelOrderDao;
	
	/**
	 * @Title: getChannelOrderInfoById 
	 * @Description: 根据订单id查询订单详细
	 * @author SUNZHICHENG  
	 * @date  2016年9月12日 下午8:37:26
	 * @since v2.5.0
	 */
	
	@Override
	public ResChannelOrderDTO getChannelOrderInfoById(ReqChannelOrderDTO reqChannelOrderDTO) {
		
		return  channelOrderDao.getChannelOrderInfoById(reqChannelOrderDTO);
		
	}
	
	/**
	 * @Title: getChannelOrderListByCriteria 
	 * @Description: 根据条件查询当前渠道所有订单
	 * @author SUNZHICHENG  
	 * @date  2016年9月13日 上午9:55:47
	 * @since v2.5.0
	 */
	
	@SuppressWarnings("unchecked")
	@Override
	public ResChannelOrdersListDTO getChannelOrderListByCriteria(ReqChannelOrdersDTO reqChannelOrdersDTO) {
		
		
		ResChannelOrdersListDTO resTravelordersListDTO=new ResChannelOrdersListDTO();
		
		//查询正式表订单列表
		List<ResChannelOrdersDTO> getChannelOrders = channelOrderDao.getChannelOrderListByCriteria(reqChannelOrdersDTO);
		//查询临时表订单列表
		List<ResChannelOrdersDTO> getChannelOrdertList = channelOrderDao.getChannelOrdertListByCriteria(reqChannelOrdersDTO);
		
		//查询正式表订单总条数
		ResChannelOrdersListDTO queryTotal = channelOrderDao.getTotal(reqChannelOrdersDTO);
		//查询临时表订单总条数
		ResChannelOrdersListDTO queryTotalOrdert= channelOrderDao.getTotalOrdert(reqChannelOrdersDTO);
		
		//将订单列表和总页数添加到响应bean
		List<ResChannelOrdersDTO> list=new ArrayList<>();
		for (ResChannelOrdersDTO resChannelOrdersDTO : getChannelOrders) {
			list.add(resChannelOrdersDTO);
		}
		
		for (ResChannelOrdersDTO resChannelOrdersDTO : getChannelOrdertList) {
			list.add(resChannelOrdersDTO);
		}
		
		//按发单时间倒序排序集合中的数据
	    Collections.sort(list, new Comparator<ResChannelOrdersDTO>() {

			@Override
			public int compare(ResChannelOrdersDTO o1, ResChannelOrdersDTO o2) {
				
				return o2.getCreateTime().compareTo(o1.getCreateTime());
			}
		});
		
		
		resTravelordersListDTO.setTotal(queryTotal.getTotal().add(queryTotalOrdert.getTotal()));		
		resTravelordersListDTO.setResChannelOrderlist(list);
		

		return resTravelordersListDTO;
	}

	
}
